package com.qen.wrapper.support;

import cn.hutool.core.util.StrUtil;
import com.qen.common.ClassUtils;

import java.lang.invoke.SerializedLambda;

/**
 * LambdaMeta的默认实现类
 *
 * @author licz
 */
public class ReflectLambdaMeta implements LambdaMeta {

    private final String SEMICOLON = ";";
    private final SerializedLambda lambda;

    private final ClassLoader classLoader;

    public ReflectLambdaMeta(SerializedLambda lambda, ClassLoader classLoader) {
        this.lambda = lambda;
        this.classLoader = classLoader;
    }

    @Override
    public String getImplMethodName() {
        return lambda.getImplMethodName();
    }

    @Override
    public Class<?> getInstantiatedClass() {
        String instantiatedMethodType = lambda.getInstantiatedMethodType();
        String instantiatedType = instantiatedMethodType.substring(2, instantiatedMethodType.indexOf(SEMICOLON))
                .replace(StrUtil.SLASH, StrUtil.DOT);
        return ClassUtils.toClassConfident(instantiatedType, this.classLoader);
    }

}
